<?php
//define('FPDF_FONTPATH', 'ufpdf/font/');
require('fpdf/fpdf.php');


class PDF extends FPDF{

var $depart = "";
var $date = "";
var $widthh = array(25,30,20,30,30,70,40,25);
var $xxx;
var $yyy;
var $pbreak = false;

function getMax($dat){
    $max = $dat[0];
    foreach($dat as $d){
        if($d>$max) $max = $d;
    }
    return $max;
}

function dateFormat($data){
    $dataX = explode("/", $data);
    return iconv("UTF-8","ISO-8859-11","วันที่ ".$dataX[0]." เดือน ".$this->getMonth($dataX[1])." พ.ศ. ".$dataX[2]);

}

function getMonth($m){

    if($m==1) return "มกราคม";
    else if($m==2) return "กุมภาพันธ์";
    else if($m==3) return "มีนาคม";
    else if($m==4) return "เมษายน";
    else if($m==5) return "พฤษภาคม";
    else if($m==6) return "มิถุนายน";
    else if($m==7) return "กรกฎาคม";
    else if($m==8) return "สิงหาคม";
    else if($m==9) return "กันยายน";
    else if($m==10) return "ตุลาคม";
    else if($m==11) return "พฤศจิกายน";
    else if($m==12) return "ธันวาคม";

}

function Header(){
    $this->SetFont('tahoma','',12);
    $this->SetTextColor(0,0,0);
    $this->setXY(15,10);
    $this->Cell(200,10,iconv("UTF-8","ISO-8859-11","ทะเบียนหนังสือรับ ").$this->depart,0,0);
    $this->Cell(65,10,$this->dateFormat($this->date),0,0,'R');
    $this->setXY(15,20);

}

function printHeader(){
    $this->SetFont('tahoma','',9);
    $this->SetTextColor(0,0,0);    
    $width = $this->widthh;
    $header = array(iconv("UTF-8","ISO-8859-11","เลขทะเบียนรับ"),
iconv("UTF-8","ISO-8859-11","ที่"),
iconv("UTF-8","ISO-8859-11","ลงวันที่"),
iconv("UTF-8","ISO-8859-11","จาก"),
iconv("UTF-8","ISO-8859-11","ถึง"),
iconv("UTF-8","ISO-8859-11","เรื่อง"),
iconv("UTF-8","ISO-8859-11","การปฏิบัติ"),
iconv("UTF-8","ISO-8859-11","หมายเหตุ"));
    for($i=0;$i<count($width);$i++){
        $x[$i] = $this->getX();
        $oy = $this->getY();
        $this->MultiCell($width[$i],8,$header[$i],0,'C');
        $y[$i] = $this->getY();
        $this->setXY($x[$i]+$width[$i],$oy);
    }
    $yy = $this->getMax($y)-$oy;
    for($i=0;$i<count($width);$i++){
        $this->setXY($x[$i],$oy);
        $this->Rect($this->getX(),$this->getY(),$width[$i],$yy);
    }
    $this->setXY($x[0],$yy+$oy);
}

function printBody($data){
    $this->SetFont('tahoma','',9);
    $this->SetTextColor(0,0,0);
    $width = $this->widthh;

    for($i=0;$i<count($width);$i++){
        $x[$i] = $this->getX();
        $oy = $this->getY();
        if($i==count($width)-1)
            $this->MultiCell($width[$i],8,"",0,'L');
        else
            $this->MultiCell($width[$i],8,$data[$i],0,'L');
        $y[$i] = $this->getY();
        $this->setXY($x[$i]+$width[$i],$oy);
    }
    $yy = $this->getMax($y)-$oy;
    for($i=0;$i<count($width);$i++){
        $this->setXY($x[$i],$oy);
        $this->Rect($this->getX(),$this->getY(),$width[$i],$yy);
    }
    $this->setXY($x[0],$yy+$oy);
}

function printBodyT($data){
    $this->SetFont('tahoma','',9);
    $this->SetTextColor(255,255,255);
    $this->xxx = $this->getX();
    $this->yyy = $this->getY();
    
    $width = $this->widthh;

    for($i=0;$i<count($width);$i++){
        $x[$i] = $this->getX();
        $oy = $this->getY();
        if($i==count($width)-1)
            $this->MultiCell($width[$i],8,"",0,'L');
        else
            $this->MultiCell($width[$i],8,$data[$i],0,'L');
        $y[$i] = $this->getY();
        $this->setXY($x[$i]+$width[$i],$oy);
    }
    $this->setXY($this->xxx,$this->yyy);
}

function AcceptPageBreak(){
    $this->pbreak = true;
    $this->setXY($this->xxx,$this->yyy);
    return false;

}

function addTable($data,$departN){
    $this->depart = $departN;
    $this->date = $data[0][7];
    $this->AddPage();
    $this->printHeader();
    for($i=0;$i<count($data);$i++){
        $this->pbreak = false;
        if($data[$i][7]!=$this->date){
            $this->date = $data[$i][7];
            $this->AddPage();
            $this->printHeader();
        }
        $this->printBodyT($data[$i]);
        if(!$this->pbreak)
            $this->printBody($data[$i]);
        else{
            $this->AddPage();
            $this->printHeader();
            $this->printBody($data[$i]);
        }
        if($this->getY()<175) continue;
        if(isset($data[$i+1]) && $data[$i+1][7]==$this->date){
            $this->AddPage();
            $this->printHeader();
        }
    }
}

}
